package cn.itcast.controller;

import cn.itcast.constant.MessageConstant;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.QueryPageBean;
import cn.itcast.entity.Result;
import cn.itcast.pojo.Role;
import cn.itcast.pojo.User;
import cn.itcast.service.RoleService;
import cn.itcast.utils.MD5Utils;
import com.alibaba.dubbo.config.annotation.Reference;
import com.sun.media.jfxmedia.logging.Logger;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.naming.Name;
import javax.websocket.server.PathParam;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping(path = "/role")
@CrossOrigin
public class RoleController {

    @Reference
    private RoleService roleService;

    //分页查询
    @GetMapping("/page")
    public Result findPage(QueryPageBean queryPageBean) {
        log.info("Role findPage方法进入了");
        try {
            PageResult pageResult = roleService.findPage(queryPageBean);
            Result result = new Result(true, MessageConstant.QUERY_ROLE_SUCCESS, pageResult);
            return result;
        } catch (Exception e) {
            log.error("Role 查询方法出错了", e);
        }
        return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
    }


    //分页查询role信息
    @GetMapping("/all")
    public Result findAllRole() {

        try {
            List<Role> roleList = roleService.findAllRole();

            Result result = new Result(true, MessageConstant.QUERY_ROLE_SUCCESS, roleList);
            return result;
        } catch (Exception e) {
            log.error("Role 查询方法出错了", e);
        }
        return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
    }

    //根据id查询role
    @GetMapping("/{row.id}")
    public Result findRoleById(@PathVariable("row.id") Integer roleId) {
        try {
            Role role = this.roleService.findRoleById(roleId);
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS, role);
        } catch (Exception e) {
            log.error("根据userById查询用户失败了", e);
        }
        return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
    }

    //根据roleId查询对应的menuIds
    @GetMapping("/{row.id}/menu")
    public Result findMenuIds(@PathVariable("row.id") Integer roleId) {
        try {
            ArrayList<Integer> menuIds = this.roleService.findMenuIds(roleId);
            return new Result(true, MessageConstant.QUERY_MENU_SUCCESS, menuIds);
        } catch (Exception e) {
            log.error("根据userById查询用户失败了", e);
        }
        return new Result(false, MessageConstant.QUERY_MENU_FAIL);
    }

    //根据roleId查询对应的permissionIds
    @GetMapping("/{row.id}/permission")
    public Result findPermissionIds(@PathVariable("row.id") Integer roleId) {
        try {
            ArrayList<Integer> permissionIds = this.roleService.findPermissionIds(roleId);
            return new Result(true, MessageConstant.QUERY_PERMISSION_SUCCESS, permissionIds);
        } catch (Exception e) {
            log.error("根据userById查询用户失败了", e);
        }
        return new Result(false, MessageConstant.QUERY_PERMISSION_FAIL);
    }


    //新建角色
    @PostMapping
    public Result handleCreate(@PathParam("permissionIds") Integer[] permissionIds,
                               @PathParam("menuIds") Integer[] menuIds,
                               @RequestBody Role role) {
        try {

            //添加数据
            this.roleService.addRole(role, permissionIds, menuIds);

            return new Result(true, MessageConstant.ADD_ROLE_SUCCESS);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.ADD_ROLE_FAIL);
    }

    //编辑
    @PutMapping
    public Result handleEdit(@PathParam("permissionIds") Integer[] permissionIds,
                             @PathParam("menuIds") Integer[] menuIds,
                             @RequestBody Role role) {
        try {
            this.roleService.editRole(role, permissionIds, menuIds);

            return new Result(true, MessageConstant.EDIT_ROLE_SUCCESS);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.EDIT_ROLE_FAIL);
    }




    //删除
    @DeleteMapping("/{row.id}")
    public Result deleteUser(@PathVariable("row.id") Integer roleId) {
        try {
            this.roleService.deleteRoleById(roleId);
            Result result = new Result(true, MessageConstant.DELETE_ROLE_SUCCESS);
            return result;
        } catch (Exception e) {
            log.error("删除用户失败了，roleId={}", roleId);
        }
        return new Result(false, MessageConstant.DELETE_ROLE_FAIL);
    }
}
